﻿// CustomScrollBar.qml
import QtQuick 2.14
import QtQuick.Controls 2.14

Rectangle {
    id: scrollBar
    width: 20
    color: "#333333"
    radius: 5
    property real contentHeight // 滚动内容的高度
    property real viewportHeight // 视口的高度
    property real position // 滚动条当前的位置
    property real maximum // 滚动条可以移动的最大距离

    Rectangle {
        id: slider
        width: parent.width
        height: (viewportHeight / contentHeight) * parent.height
        color: "#555555"
        radius: 5
        anchors.top: parent.top
        anchors.topMargin: position
    }

    Binding {
        target: slider
        property: "anchors.topMargin"
        value: scrollBar.position
        when: scrollBar.contentHeight > scrollBar.viewportHeight
    }

    Binding {
        target: scrollBar
        property: "maximum"
        value: contentHeight - viewportHeight
        when: contentHeight > viewportHeight
    }

    MouseArea {
        anchors.fill: parent
        onPositionChanged: {
            var deltaY = mouse.prevY - mouse.y;
            scrollBar.position += deltaY * (scrollBar.height / scrollBar.maximum) * viewportHeight;
            scrollBar.position = Math.max(0, Math.min(scrollBar.maximum, scrollBar.position));
            // 这里需要触发滚动内容的相应移动，可以通过信号或其他机制实现
        }
    }

    // 添加信号来通知外部滚动位置的变化
    signal scrollPositionChanged(real newPosition)

    onPositionChanged: {
        positionChanged(position);
    }
}
